﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EQual.MainModule.Documents.DetailViews
{
    /// <summary>
    /// Interaction logic for RolesDialog.xaml
    /// </summary>
    public partial class RolesDialog : Window
    {
        public RolesDialog()
        {
            InitializeComponent();          
     

        }
        IList<CheckBox> boxes;
        IList<DataAccesLayer.Role> selectedRoles;

        public RoleDestributionPresenter presenter { get; set; }

        public object Model
        {
            get { return selectedRoles; }
            set
            {
                selectedRoles = value as IList<DataAccesLayer.Role>;
                SetUpWindow();
                SetUpRoles(value as IList<DataAccesLayer.Role>); 
                AddEventHandlers();

            }
        }
     
        private void SetUpWindow()
        {
            IList<DataAccesLayer.Role> roles = new DataAccesLayer.EQualDataLayerFactory().GetEmployeeAccesLayer().GetRoles();

            boxes = new List<CheckBox>();

            foreach (DataAccesLayer.Role role in roles)
            {
                StackPanel box = new StackPanel(); 
                box.Name = role.Name;

                Label id_l = new Label(); 
                id_l.Content = role.Name;

                CheckBox id_c = new CheckBox();
                
                id_c.Name = role.Name;
                id_c.Tag = role;

                box.Children.Add(id_l); 
                box.Children.Add(id_c);
                boxes.Add(id_c);

                this.dockPanel1.Children.Add(box);
            }
        }

        private void AddEventHandlers()
        {
            foreach(CheckBox id_c in boxes )
            {id_c.Checked += new RoutedEventHandler(id_c_Checked);
            id_c.Unchecked += new RoutedEventHandler(id_c_Unchecked);
            }
        }

        void id_c_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox combo = sender as CheckBox;
            presenter.RemoveRoleToDocument(combo.Tag);
        }


        protected void SetUpRoles(IList<DataAccesLayer.Role> roles)
        {
            foreach (DataAccesLayer.Role role in roles)
                foreach (CheckBox box in boxes)
                    if ((box.Tag as DataAccesLayer.Role).Name == role.Name)
                    {
                        box.IsChecked = true;
                    }
        }

        void id_c_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox combo = sender as CheckBox;

            presenter.AddRoleToDocument(combo.Tag);


        }

        void calcel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void ok_Click(object sender, RoutedEventArgs e)
        {
          selectedRoles   =new List<DataAccesLayer .Role >();
            foreach (CheckBox box in boxes)
                if (box.IsChecked == true)
                    selectedRoles .Add(box.Tag as DataAccesLayer .Role  );
            
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {

        }

    }
}
